package com.szewec.ps.ui.tabbar;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.szewec.ps.R;
import com.szewec.ps.base.CommonDialog;
import com.szewec.ps.base.Constants;
import com.szewec.ps.base.Enums;
import com.szewec.ps.model.PresentValue;
import com.szewec.ps.model.response.ResponseData;
import com.szewec.ps.myview.toast.bamtoast.BamToast;
import com.szewec.ps.ui.tabbar.beans.PlanTask;
import com.szewec.ps.ui.task.SelectFacilityTypeActivity;
import com.szewec.ps.util.StringUtils;
import com.szewec.ps.util.okhttp.OkHttp;

import java.util.HashMap;
import java.util.Map;

public class StartDialog extends Dialog {
    private TextView btnStart;
    private TextView title;
    private TextView end;
    private TextView continuance;

    private PlanTask planTask;
    private Context context;

    private boolean isClick = false;

    public StartDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_start_btn);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(true);
        //设置背景透明
//        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //初始化界面控件
        Window window = getWindow();
        window.setBackgroundDrawableResource(android.R.color.white);
        window.setWindowAnimations(R.style.main_menu_animstyle);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.getDecorView().setPaddingRelative(0, 0, 0, 0);
        window.setAttributes(getLayoutParams(window.getAttributes()));
        initView();
    }

    private void initView() {
        btnStart = findViewById(R.id.btn_start);
        title = findViewById(R.id.tv_title);
        continuance = findViewById(R.id.btn_continue);
        end = findViewById(R.id.btn_end);

        setViewData();

        btnStart.setOnClickListener(v -> {
            if (planTask.getTaskStatus() == Enums.TaskStatusEnum.NOT_STARTED.getKey()) {
                planTask.setTaskStatus((Integer) Enums.TaskStatusEnum.UNDER_WAY.getKey());
            } else if (planTask.getTaskStatus() == Enums.TaskStatusEnum.UNDER_WAY.getKey()) {
                planTask.setTaskStatus((Integer) Enums.TaskStatusEnum.SUSPEND.getKey());
            } else if (planTask.getTaskStatus() == Enums.TaskStatusEnum.SUSPEND.getKey()) {
                planTask.setTaskStatus((Integer) Enums.TaskStatusEnum.UNDER_WAY.getKey());
            }

            saveData();
            isClick = true;
        });

        continuance.setOnClickListener(v -> jump());

        end.setOnClickListener(v -> {
            CommonDialog commonDialog = new CommonDialog(getContext());
            commonDialog.setTitle(this.getContext().getResources().getString(R.string.confirm_hint));
            commonDialog.setContent("是否结束当前任务，结束后将不可继续进行");
            commonDialog.setCancel("取消");
            commonDialog.setEnsure("确定");
            commonDialog.setOnEnsureClickListener(v1 -> {
                planTask.setTaskStatus((Integer) Enums.TaskStatusEnum.COMPLETED.getKey());
                saveData();
                dismiss();
                commonDialog.dismiss();
            });
            commonDialog.setOnCancelClickListener((view) -> commonDialog.dismiss());
            commonDialog.show();
        });
    }

    private void jump() {
        if (planTask.getTaskStatus() == Enums.TaskStatusEnum.UNDER_WAY.getKey()) {
            Intent intent = new Intent(getContext(), SelectFacilityTypeActivity.class);
            Map<String, Object> map = new HashMap<>();
            if (StringUtils.isEmpty(planTask.getId())) {
                BamToast.showText(getContext(), "数据有误，请联系管理员", false);
                return;
            }
            map.put("id", planTask.getId());
            PresentValue<Map<String, Object>> presentValue = new PresentValue<>(Constants.PRESENT_VALUE, map);
            intent.putExtra(Constants.EXTRA_KEY.SELECT_FACILITY_TYPE, presentValue);
            getContext().startActivity(intent);
        }
    }

    private void saveData() {
        OkHttp.put("/tpms-plan-service/api/task/simple-put/", planTask, result -> {
            ResponseData res = JSON.parseObject(result,ResponseData.class);
            if (res.getCode() == 10000) {
                setViewData();
                if (isClick) {
                    jump();
                }
            }
            System.out.println(result);
        });
    }

    @SuppressLint("SetTextI18n")
    private void setViewData() {
        if (planTask.getTaskStatus() == Enums.TaskStatusEnum.NOT_STARTED.getKey()) {
            title.setText(planTask.getTaskMatter() + Enums.TaskStatusEnum.NOT_STARTED.getValue() + "...");
            btnStart.setText("开始");
            btnStart.setBackgroundResource(R.drawable.circle_btn_start_bg);
            end.setVisibility(View.GONE);
            continuance.setVisibility(View.VISIBLE);
        } else if (planTask.getTaskStatus() == Enums.TaskStatusEnum.UNDER_WAY.getKey()) {
            title.setText(planTask.getTaskMatter() + Enums.TaskStatusEnum.UNDER_WAY.getValue() + "...");
            btnStart.setText("暂停");
            btnStart.setBackgroundResource(R.drawable.circle_btn_pause_bg);
            end.setVisibility(View.VISIBLE);
            continuance.setVisibility(View.VISIBLE);
        } else if (planTask.getTaskStatus() == Enums.TaskStatusEnum.SUSPEND.getKey()) {
            title.setText(planTask.getTaskMatter() + Enums.TaskStatusEnum.SUSPEND.getValue() + "...");
            btnStart.setText("开始");
            btnStart.setBackgroundResource(R.drawable.circle_btn_start_bg);
            end.setVisibility(View.VISIBLE);
            continuance.setVisibility(View.GONE);
        }
    }

    protected WindowManager.LayoutParams getLayoutParams(WindowManager.LayoutParams params) {
        if (params == null) {
            return new WindowManager.LayoutParams();
        }
        //注意这里设置的宽高，需要设置成MATCH_PARENT,不然的话就不会起作用，就这一点坑。
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.gravity = Gravity.BOTTOM;// 紧贴底部
        return params;
    }

    public PlanTask getPlanTask() {
        return planTask;
    }

    public StartDialog setPlanTask(PlanTask planTask) {
        this.planTask = planTask;
        return this;
    }
}
